<?php

namespace app\admin\framework\controller;

use app\admin\framework\api\Permission;
use app\admin\framework\validate\UserValidate;
use app\common\model\User;
use app\common\service\UserService;

#[Permission('用户列表', 'User')]
class UserController
{
    
    public $noNeedAuth = ['profile'];
    
    public function profile()
    {
        $profile = UserService::get();
        if (empty($profile)) {
            return apiError('获取失败');
        }
        
        if (request()->method() === 'GET') {
            $profile->hidden(['balance', 'score']);
            $profile->permissions = UserService::getRules($profile->id);
            return apiSuccess(data: $profile);
        }
        
        
        if (request()->method() === 'POST') {
            $data = request()->post();
            print_r($data);
            if (!empty($data['password'])) {
                $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            }else{
                unset($data['password']);
            }
            
            $profile->update($data);
            return apiSuccess('修改成功');
        }
    }
    
    
    
    #[Permission('详情')]
    public function detail()
    {
        $id = request()->get('id', 0);
        $data = User::where('id', $id)->hidden(['balance', 'score', 'password'])->find()->toArray();
        return apiSuccess(data: $data);
    }
    
    #[Permission('查询')]
    public function select()
    {
        $data = request()->get();
        $list = User::where(function ($query) use ($data) {
            if (!empty($data['keyword'])) {
                $query->where('username|nickname|mobile', 'like', '%' . $data['keyword'] . '%');
            }
            
            $query->where('user_type', 2);
        })->hidden(['password'])->order('id desc')->paginate();
        return apiSuccess(data: ['list' => $list->items(), 'total' => $list->total()]);
    }
    
    #[Permission('创建')]
    public function create()
    {
        if (request()->method() === 'PUT') {
            $data = request()->except(['balance', 'score']);
            $validate = new UserValidate();
            if (!$validate->scene('admin')->check($data)) {
                return apiError($validate->getError());
            }
            if (!empty($data['password'])) {
                $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            }
            $data['create_ip'] = request()->getRealIp();
            (new User())->save($data);
            return apiSuccess('保存成功');
        }
        return apiError('保存失败');
    }
    
    #[Permission('更新')]
    public function update()
    {
        if (request()->method() === 'PUT') {
            $data = request()->except(['balance', 'score']);
            $validate = new UserValidate();
            if (!$validate->scene('admin')->check($data)) {
                return apiError($validate->getError());
            }
            if (!empty($data['password'])) {
                $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            } else {
                unset($data['password']);
            }
            (new User())->update($data);
            return apiSuccess('保存成功');
        }
        return apiError('保存失败');
    }
    
    #[Permission('快捷修改')]
    public function speed()
    {
        if (setFieldUpdate(new User())) {
            return apiSuccess('修改成功');
        } else {
            return apiError('修改失败');
        }
    }
    
    #[Permission('删除')]
    public function delete()
    {
        if (request()->method() === 'DELETE') {
            $id = request()->get('id', 0);
            if (User::destroy($id) !== false) {
                return apiSuccess('删除成功');
            } else {
                return apiError('删除失败');
            }
        }
        return apiError('删除失败');
    }
    
}